// pages/login/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    password:"",
    phoneError:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  login() {
    if (this.data.phone == "" || this.data.phoneError != "") {
      wx.showToast({
        title: '登录信息有误，请检查一下',
        icon: 'none',
        duration: 1500
      });
      return;
    }

    wx.showLoading({
      title: '加载中',
    });
    //提交信息
    let that = this
    wx.request({
      url: getApp().globalData.BASE_URL + "/login/admin",
      method: "GET",
      data: {
        username: this.data.phone,
        password: this.data.password
      },
      success(res) {
        res = getApp().responseInterceptor(res)
        if (res === undefined) return;
        wx.setStorage({
          key: getApp().globalData.STORAGE_NAME,
          data: res.token
        });
        getApp().judgeLoginStatus(() => {
          wx.switchTab({
            url: '../../home/index/index'
          })
        })
        wx.hideLoading();
      }
    })
  },
  wxlogin(){
    let that = this
    wx.login({
      success: response => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.showLoading({
          title: '加载中',
        });
        //提交信息
        wx.request({
          url: getApp().globalData.BASE_URL + "/login/admin",
          method: "GET",
          data: {
            code: response.code
          },
          success(res) {
            res = getApp().responseInterceptor(res)
            if (res === undefined) return;
            wx.setStorage({
              key: getApp().globalData.STORAGE_NAME,
              data: res.token
            });
            getApp().judgeLoginStatus(() => {
              wx.switchTab({
                url: '../../home/index/index'
              })
            })
            wx.hideLoading();
          }
        })
      }
    })
  },

  checkPhone(e){
    
    let value = e.detail.value;
    let message  = "";
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-3]{1})|(15[4-9]{1})|(18[0-9]{1})|(199))+\d{8})$/;
    if (value == '') {
      message = "手机号码不能为空！";
    } else if (value.length != 11) {
      message = "请输入11位手机号码！";
    } else if (!myreg.test(value)) {
      message = "请输入有效的手机号码！";
    }
    this.setData({
      phoneError:message
    })
  
  },
  changePhone(event) {
    this.setData({
      phone: event.detail,
    });
  },
  changePassword(event) {
    this.setData({
      password: event.detail,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //给logo加入动画
    this.rotateLogo();
    setInterval(this.rotateLogo, 5000);
  },

  rotateLogo(){
    this.animate('#logo', [
      { rotate: 0},
      { rotate: 35},
      { rotate: 0},
      { rotate: -35 },
      { rotate: 0 },
    ], 5000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})